package com.mojavelinux.open18;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

@Entity
@Table( name = "round" )
public class Round implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	private long id;

	private Date date;

	private TeeSet teeSet;

	private String weather;

	private String notes;
	
	private Set<Score> scores;

	private int totalScore;
	
	public Round() {
		// FIXME: make this a TreeSet
		scores = new LinkedHashSet<Score>();
	}

	@Id
	@GeneratedValue
	@Column( name = "id", unique = true, nullable = false )
	@NotNull
	public long getId() {
		return this.id;
	}

	public void setId( long id ) {
		this.id = id;
	}

	@Temporal( TemporalType.DATE )
	@Column( name = "date", length = 10 )
	public Date getDate() {
		return this.date;
	}

	public void setDate( Date date ) {
		this.date = date;
	}

	public TeeSet getTeeSet() {
		return teeSet;
	}

	public void setTeeSet( TeeSet teeSet ) {
		this.teeSet = teeSet;
	}

	@Column( name = "weather", nullable = false, length = 8 )
	@NotNull
	@Length( max = 8 )
	public String getWeather() {
		return this.weather;
	}

	public void setWeather( String weather ) {
		this.weather = weather;
	}

	/**
	 * Notes about the round.  Perhaps a ranking in
	 * a tournament, something odd about the course,
	 * or mental reminders or lessons learned.
	 */
	@Column( name = "notes", length = 65535 )
	@Length( max = 65535 )
	public String getNotes() {
		return this.notes;
	}

	public void setNotes( String comment ) {
		this.notes = comment;
	}

	@OneToMany( cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "round" )
	//@OrderBy("number asc")
	public Set<Score> getScores() {
		return scores;
	}

	public void setScores( Set<Score> scores ) {
		this.scores = scores;
	}
	
	/**
	 * A calculated value stored for convenience when displaying rounds.
	 */
	@Column( name="total_score" )
	public int getTotalScore() {
		return totalScore;
	}

	public void setTotalScore( int totalScore ) {
		this.totalScore = totalScore;
	}

}
